package color.koitq.picker.base;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hjq.permissions.XXPermissions;
import com.qmuiteam.qmui.arch.QMUIFragment;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseFragment extends QMUIFragment {

    protected abstract int getLayoutId();

    private QMUITipDialog loadingDialog;
    private QMUITipDialog tipDialog;

    protected FragmentActivity mActivity;
    protected Context mContext;

    private boolean isFirst = true;

    @Override
    protected View onCreateView() {
        mActivity = getActivity();
        mContext = getContext();
        View view = LayoutInflater.from(mActivity).inflate(getLayoutId(), null);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    protected void onViewCreated(@NonNull View rootView) {
        super.onViewCreated(rootView);
        if (isFirst) {
            initKotlinWidget();
            isFirst = false;
        }
    }

    protected void init() {

    }

    protected void initKotlinWidget() {

    }

    protected void turnSystemPermissionBack() {
        System.out.println("BaseFragment-turnSystemPermissionBack");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE) {
            turnSystemPermissionBack();
        }
    }

    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
        if (tipDialog != null) {
            tipDialog.cancel();
            tipDialog = null;
        }
        if (loadingDialog != null) {
            loadingDialog.cancel();
            loadingDialog = null;
        }
    }

    protected void showLoading(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create();
        loadingDialog.show();
    }

    protected void showLoadingC(String msg) {
        loadingDialog = null;
        loadingDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord(msg)
                .create(false);
        loadingDialog.show();
    }

    protected void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    protected void showErrorTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, BaseActivity.TIP_DURATION);
    }

    protected void showSuccessTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, BaseActivity.TIP_DURATION);
    }

    protected void showNormalTip(View view, String msg) {
        tipDialog = new QMUITipDialog.Builder(getActivity())
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_INFO)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        view.postDelayed(this::dismissTip, BaseActivity.TIP_DURATION);
    }

    private void dismissTip() {
        if (tipDialog != null && tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }
}
